package com.skivingcloud.admin.utils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.math.BigDecimal;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.SimpleSession;

import com.skivingcloud.admin.sys.entity.User;
import com.skivingcloud.common.contants.Constant;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class UserUtil {
	

	//操作人
	public static String getUserName() {
		Session session = SecurityUtils.getSubject().getSession();
		User user = (User) session.getAttribute(Constant.USERSESSION);
		String username = user.getUsername();
		return username;
	}
	//当前登录人名称
	public static String getStaffName() {
		Session session = SecurityUtils.getSubject().getSession();
		User user = (User) session.getAttribute(Constant.USERSESSION);
		String staffName = user.getStaffName();
		return staffName;
	}
	//当前登录人部门编号
	public static String getOrgId() {
		Session session = SecurityUtils.getSubject().getSession();
		User user = (User) session.getAttribute(Constant.USERSESSION);
		String orgId = user.getOrgId();
		return orgId;
	}
	
	// 获取当前的登陆用户对象
	public static User getUser() {
		Session session = SecurityUtils.getSubject().getSession();
		User user = (User) session.getAttribute(Constant.USERSESSION);
		return user;
	}
	
	 public static void main(String[] args) {
		//创建连接池对象  host:连接redis主机IP ；port:redis的默认端口
		 JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), "127.0.0.1", 6379, 3000000, "1qaz!QAZ3edc", 0);
		//获取连接资源
		 Jedis resource = jedisPool.getResource();
		 byte[] hello = resource.get("shiro:session:89e96c22-4862-4001-8da7-62db8656eb6e".getBytes());//.get("shiro:session:6385811a-7498-45cf-abb9-cda2f9339864");
	 	
	 	System.out.println("----------"+hello);
	 	resource.close();
	 }
	 
	// 把byte还原为session
	    public static Session byteToSession(byte[] bytes){
	        ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
	        ObjectInputStream in;
	        SimpleSession session = null;
	        try {
	            in = new ObjectInputStream(bi);
	            session = (SimpleSession) in.readObject();
	        } catch (ClassNotFoundException e) {
	            e.printStackTrace();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	    
	        return session;
	    }
	    
	    /**
	     * 这个用户代码表信息系统 
	     * @return
	     */
	    public static User getSystem() {
	    	return new User("-1", "systemSelf", null, null, null, null, "100201", "1", null, "-1", "1", "信息系统", "信息系统", null, null, "1", null, null, new BigDecimal(0), null, new BigDecimal(5), null, new BigDecimal(1), null, "1", "blue", "-1", "1", "1");
	    }
}
